﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using TTGame;
using CinemaDirector;

/*
 * PlayParticleTrigger
 *
 *
 *
 * 依赖：
 *
 * ！不写重复代码！
 * @author TTun
 * @date 8/8/2017 8:59:49 AM
 */
namespace TTGameEngine {

[CutsceneItemAttribute("LT Trigger", "Play Particle Trigger", CutsceneItemGenre.ActorItem)]
public class PlayParticleTrigger : CinemaActorEvent {
    public string particleName;
    public float timeToRemove = 0; //持续时间，时间结束自动删除。为0，表示不删除。
    public string playPosAtObjPos; //相对于人物Pos下的节点位置。
    public bool IsRemoveEffectOnSkillEnd = true;//技能结束时删除特效。

    ObjRecycleTimer timer;
    int timerID = 0;
    bool originActive = false;
    Puppet obj;
    GameObject effect;
    public override void Trigger(GameObject actor) {
        SkillTL skill = this.GetSkill();
        //fixed:停止技能脚本时，脚本上所有剩余的触发器都会触发一次。
        if(skill.IsStopped()) { return; }

        obj = actor.GetComponent<Puppet>();
        if(obj == null) {
            return;
        }

        //muzzleTF
        Transform relationTF = obj.GetPosTF(playPosAtObjPos);

        GameObject prefab = GameObjectLoader.Ins.GetPrefabObj(GameConstant.PrefabPath_Effect + particleName) as GameObject;
        if(prefab == null) {
            return;
        }
        effect = ObjectRecycler.Instance.InstantiatePrefab(prefab, Vector3.zero, Quaternion.identity);
        effect.transform.parent = relationTF;
        effect.transform.localPosition = Vector3.zero;
        effect.transform.localRotation = Quaternion.identity;

        effect.SetLayerInChildren(obj.GetEffectLayerID());
        timer = effect.AddComponentSafe<ObjRecycleTimer>();
        timer.LifeTime = timeToRemove;
        timerID = timer.RecycleID;
        originActive = effect.activeSelf;
        obj.AttachEffect(effect);
    }

    public override void Stop(GameObject Actor) {
        if(obj != null) {
            obj.UnattachEffect(effect);
        }
        if(IsRemoveEffectOnSkillEnd) {
            if(timer != null) timer.RecycleObjSafe(timerID);
        }
    }

}

}
